﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace StuManageSys
{
    public partial class Form1 : Form
    {
        int oprateObj;

        public Form1()
        {
            InitializeComponent();
            panel1.Location = new Point(0, 100);
            panel2.Location = new Point(0, 100);
            panel3.Location = new Point(20, 100);
            panel4.Location = new Point(20, 100);
            panel5.Location = new Point(20, 100);
            panel6.Location = new Point(100, 100);
            panel7.Location = new Point(150, 100);
            panel8.Location = new Point(150, 100);
            panel2.Visible = false;
            panel3.Visible = false;
            panel4.Visible = false;
            panel5.Visible = false;
            panel6.Visible = false;
            panel7.Visible = false;
            panel8.Visible = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            sqliteController.createTable();
            sqliteController.createLogTable();
        }

        private void school_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            panel2.Visible = true;
            oprateObj = 1;
        }

        private void banji_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            panel2.Visible = true;
            oprateObj = 2;
        }

        private void xuesheng_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            panel2.Visible = true;
            oprateObj = 3;
        }

        private void rizhi_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            panel6.Visible = true;
            label8.Text = "操作日志：";
            textBox1.Font = new Font("宋体", 12);
            textBox1.Lines = sqliteController.queryLog().ConvertAll(x => x.ToString()).ToArray();
        }

        private void fanhuiPenal1_Click(object sender, EventArgs e)
        {
            panel2.Visible = false;

            panel1.Visible = true;
        }

        private void xinzeng_Click(object sender, EventArgs e)
        {
            panel2.Visible = false;
            if (oprateObj == 1)
                panel3.Visible = true;
            else if (oprateObj == 2)
                panel4.Visible = true;
            else if (oprateObj == 3)
                panel5.Visible = true;
        }

        private void xiugai_Click(object sender, EventArgs e)
        {
            panel8.Visible = true;
            panel2.Visible = false;
        }

        private void cahxun_Click(object sender, EventArgs e)
        {
            panel2.Visible = false;
            textBox1.Font = new Font("宋体", 18);
            List<string> queryResult;
            if (oprateObj == 1)
            {
                label8.Text = "学校信息：";
                queryResult = sqliteController.querySchool();
                textBox1.Lines = queryResult.ConvertAll(x => x.ToString()).ToArray();
            }
            else if (oprateObj == 2)
            {
                label8.Text = "班级信息：";
                queryResult = sqliteController.queryClass();
                textBox1.Lines = queryResult.ConvertAll(x => x.ToString()).ToArray();
            }
            else if (oprateObj == 3)
            {
                label8.Text = "学生信息：";
                queryResult = sqliteController.queryStudent();
                textBox1.Lines = queryResult.ConvertAll(x => x.ToString()).ToArray();
            }

            panel6.Visible = true;
        }

        private void shanchu_Click(object sender, EventArgs e)
        {
            panel2.Visible = false;
            if (oprateObj == 1)
            {
                label10.Text = "请输入要删除的学校名称：";
            }
            else if (oprateObj == 2)
            {
                label10.Text = "请输入要删除的班级名称：";
            }
            else if (oprateObj == 3)
            {
                label10.Text = "请输入要删除的学生姓名：";
            }

            panel7.Visible = true;
        }

        private void fanhuiPenal2_Click(object sender, EventArgs e)
        {
            panel3.Visible = false;
            panel2.Visible = true;
        }

        private void addxuexiao_Click(object sender, EventArgs e)
        {
            string xuexiao_name = xuexaioName.Text;
            if (xuexiao_name == "")
            {
                MessageBox.Show("学校名称不能为空！");
                return;
            }

            string xuexiao_type = xuexiaoType.Text;
            if (xuexiao_type == "")
            {
                MessageBox.Show("学校类型不能为空！");
                return;
            }

            string xuexiao_address = xuexiaoAddr.Text;
            if (xuexiao_address == "")
            {
                MessageBox.Show("学校地址不能为空！");
                return;
            }

            try
            {
                sqliteController.addSchool(xuexiao_name, xuexiao_type, xuexiao_address);
                MessageBox.Show("添加学校信息成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加学校信息时发生错误: " + ex.Message);
            }
        }

        private void nainjifanhuicaozuo_Click(object sender, EventArgs e)
        {
            panel4.Visible = false;
            panel2.Visible = true;
        }

        private void addnainji_Click(object sender, EventArgs e)
        {
            string banji_name = banjimingcheng.Text;
            if (banji_name == "")
            {
                MessageBox.Show("班级名称不能为空！");
                return;
            }

            string banji_xuexaio = suoshuxuexiao.Text;
            if (banji_xuexaio == "")
            {
                MessageBox.Show("学校名称不能为空！");
                return;
            }

            string nj = nianji.Text;
            if (nj == "")
            {
                MessageBox.Show("年级不能为空！");
                return;
            }

            try
            {
                sqliteController.addClass(banji_name, banji_xuexaio, nj);
                MessageBox.Show("添加班级信息成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加班级信息时发生错误: " + ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            panel5.Visible = false;
            panel2.Visible = true;
        }

        private void addXuesheng_Click(object sender, EventArgs e)
        {
            string xuesheng_name = xueshengXingming.Text;
            if (xuesheng_name == "")
            {
                MessageBox.Show("学生姓名不能为空！");
                return;
            }

            string xueshengID = xueshengXuehao.Text;
            if (xueshengID == "")
            {
                MessageBox.Show("学生学号不能为空！");
                return;
            }

            string xueshengbj = xueshengBanji.Text;
            if (xueshengbj == "")
            {
                MessageBox.Show("学生班级不能为空！");
                return;
            }

            string xueshengxx = xueshengxuexiao.Text;
            if (xueshengxx == "")
            {
                MessageBox.Show("学生学校不能为空！");
                return;
            }

            try
            {
                sqliteController.addStudent(xuesheng_name, xueshengID, xueshengbj, xueshengxx);
                MessageBox.Show("学生信息添加成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加学生信息时发生错误: " + ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            panel6.Visible = false;
            panel2.Visible = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            panel7.Visible = false;
            panel2.Visible = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                string name = textBox2.Text;
                if (name == "")
                {
                    MessageBox.Show("请输入要删除的内容！");
                }

                if (oprateObj == 1)
                {
                    sqliteController.delSchool(name);
                }
                else if (oprateObj == 2)
                {
                    sqliteController.delClass(name);
                }
                else if (oprateObj == 3)
                {
                    sqliteController.delStudent(name);
                }

                MessageBox.Show("删除成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show("删除失败！ " + ex.Message);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            panel8.Visible = false;
            panel2.Visible = true;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string stuName = xiugaiName.Text;
            if (stuName == "")
            {
                MessageBox.Show("姓名不能为空！");
                return;
            }

            string newClass = newclass.Text;
            if (newClass == "")
            {
                MessageBox.Show("新班级不能为空！");
                return;
            }

            string newid = newXuehao.Text;
            if (newid == "")
            {
                MessageBox.Show("新学号不能为空！");
                return;
            }

            try
            {
                sqliteController.editStudent(stuName, newid, newClass);
                MessageBox.Show("修改成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show("修改失败：" + ex.Message);
            }
        }
    }
}